/*
 * This action takes the post parameters from the front end and has them
 * posted into the database to be added to the thread the reply was 
 * written in.
 */

package airport.forum.action;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import airport.forum.pojo.Post;
//reply
//import airport.forum.pojo.Thread;

import airport.forum.dao.ThreadDAO;
//reply thread
public class ReplyThreadAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {
	//test push
	private static final long serialVersionUID = 1L;
	
	private String comments = null;
	private long threadId;
	private String username = null;
	//private String post;
	
	protected HttpServletResponse servletResponse;  
	@Override   
	public void setServletResponse(HttpServletResponse servletResponse) {   
		this.servletResponse = servletResponse;   
	}   
	
	protected HttpServletRequest servletRequest;   
	@Override   
	public void setServletRequest(HttpServletRequest servletRequest) {   
		this.servletRequest = servletRequest;   
	}
	
	public String execute() {
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		
		System.out.println("threadId:"+threadId);
		System.out.println("comments:"+comments);
		System.out.println("username:"+username);
		
		ThreadDAO dao = new ThreadDAO();
		Post post = new Post();
		post.setUserName(username);
		post.setPostText(comments);
		dao.addPostToThread(post, threadId);
		
		return "success";

	}
	
	public void validate() {
		System.out.println("Validating Reply Thread!");
		if (comments.trim().length() > 1000 || comments.trim().length() < 5){
			addFieldError("error",
					"Your post must be less that 1000 characthers and more than 10 characters");
		}
		
		
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	public long getThreadId() {
		return threadId;
	}

	public void setThreadId(long threadId) {
		this.threadId = threadId;
	}

}
